﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Whir_System_Module_Template_ColumnTemplateList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree("0");
        }
    }

    protected string GetPath(string path, string str)
    {
        if (string.IsNullOrEmpty(path))
            return "";
        else
        {
            return "<a target=\"_blank\" href=\"TemplateLabel.aspx?dir=" + Server.UrlEncode(Server.MapPath(path)) + "\">" + str + "</a>";
        }
    }

    /// <summary>
    /// 绑定栏目树
    /// </summary>    
    private void BindTree(string ParentID)
    {
        TreeView1.Nodes.Clear();
        TreeNode node = new TreeNode();
        node.Text = "导航栏目";
        node.Value = "0";
        node.PopulateOnDemand = false;
        node.SelectAction = TreeNodeSelectAction.Expand;
        node.Expanded = true;
        TreeView1.Nodes.Add(node);

        BindNode(node, ParentID);
    }

    /// <summary>
    /// 绑定节点
    /// </summary>
    private void BindNode(TreeNode node, string ParentID)
    {
        TreeNode nodeItem;
        DataSet ds = null;
        ds = Whir.SQLServerDAL.DbHelperSQL.Query("select columnID,columnName,defaultTemp,contentTemp,listtemp,isSinglePage,operatePage from Whir_Column where parentID=" + ParentID + " order by sort");
        if (ds == null) return;
        if (ds.Tables[0].Rows.Count == 0) return;

        string str = "";
        string tempindex = "", templist = "", tempcontent = "";
        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeItem = new TreeNode();

            tempindex = ds.Tables[0].Rows[i]["defaultTemp"].ToString();
            templist = ds.Tables[0].Rows[i]["listtemp"].ToString();
            tempcontent = ds.Tables[0].Rows[i]["contentTemp"].ToString();

            if (bool.Parse(ds.Tables[0].Rows[i]["isSinglePage"].ToString()))
            {
                str = GetPath(tempcontent, "&nbsp;【内容模板&nbsp;-&nbsp;" + Path.GetFileName(tempcontent) + "】");
            }
            else if (ds.Tables[0].Rows[i]["operatePage"].ToString() != "")
            {
                str = "";
            }
            else if (ds.Tables[0].Rows[i]["defaultTemp"].ToString() != "")
            {
                str = GetPath(tempindex, "&nbsp;【首页模板&nbsp;-&nbsp;" + Path.GetFileName(tempindex) + "】");
            }
            else
            {
                str = GetPath(templist, "&nbsp;【列表模板&nbsp;-&nbsp;" + Path.GetFileName(templist) + "】") + ""
                    + GetPath(tempcontent, "&nbsp;【内容模板&nbsp;-&nbsp;"+Path.GetFileName(tempcontent)+"】");
            }
            nodeItem.Text = ds.Tables[0].Rows[i]["columnName"].ToString() + "(<span title=\"栏目编号\">" + ds.Tables[0].Rows[i]["columnID"].ToString() + "</span>)" + str;
            nodeItem.Value = ds.Tables[0].Rows[i]["columnID"].ToString();
            nodeItem.SelectAction = TreeNodeSelectAction.None;
            nodeItem.Expanded = true;
            BindNode(nodeItem, ds.Tables[0].Rows[i]["columnID"].ToString());
            node.ChildNodes.Add(nodeItem);
        }
    }

    //事件
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

    }
}
